package service.impl;


import bean.CutPageBean;
import bean.UserBean;
import mapper.IUserMapper;
import service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public CutPageBean<UserBean> findAll(Integer pageNO) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findAll();
        session.close();
        return null;
    }

    @Override
    public Boolean add(UserBean userBean) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        try {
            mapper.add(userBean);
            session.commit();
        } catch (Exception e) {
            session.rollback();
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public void delete(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delete(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer userId, String newGrade) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.update(userId, newGrade);
        session.commit();
        session.close();
    }

    @Override
    public UserBean findById(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean userBean = mapper.findById(id);
        session.close();
        return userBean;
    }

    @Override
    public UserBean getByUsernameAndPwd(String name, String pwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean userBean = mapper.getByUsernameAndPwd(name, pwd);
        session.close();
        return userBean;
    }

    @Override
    public void updatePwd(Integer id, String newPwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updatePwd(id, newPwd);
        session.commit();
        session.close();
    }
}
